from django.shortcuts import render
from rest_framework.generics import ListAPIView,RetrieveAPIView
from course.models import CourseDirectionModel,CourseCategoryModel,CourseModel
from course.models import CourseDirectionModel,CourseCategoryModel,CourseModel,CourseChapterModel
from course.seriali import CourseDirectionModelSerializer,CourseCategoryModelSerializer,CourseModelSerializer,CourseRetrieveSerializer,CourseChapterSerializer
from rest_framework.pagination import PageNumberPagination


class CoursePageNumberPagination(PageNumberPagination):
    page_size = 5
    page_query_param = 'page'
    page_size_query_param = "size"
    max_page_size = 10


# 课程一级分类：方向视图
class CourseDirectionListApiView(ListAPIView):
    queryset = CourseDirectionModel.objects.filter(is_show=True,is_delete=False).order_by('-id')
    serializer_class = CourseDirectionModelSerializer

# id
# 二级分类：课程分类视图
class CourseCategoryListApiView(ListAPIView):
    serializer_class = CourseCategoryModelSerializer
    # queryset = CourseCategoryModel.objects.filter(is_show=True,is_deleted=False).order_by('-id')
    def get_queryset(self):
        print('-----------')
        print(self.kwargs)
        directionID = int(self.kwargs.get('directionID',0))
        query_set = CourseCategoryModel.objects.filter(is_show=True,is_delete=False).order_by('-id')
        if directionID > 0:
            query_set = query_set.filter(direction=directionID)
        return query_set



from rest_framework.filters import OrderingFilter




# 课程列表视图
# APIView
class CourseListApiView(ListAPIView):
    serializer_class = CourseModelSerializer
    filter_backends = [OrderingFilter]  # 局部指定过滤器
    ordering_fields = ['id', 'price', 'students']
    pagination_class = CoursePageNumberPagination
    # queryset = 结果集
    def get_queryset(self):

        directionID = int(self.kwargs.get('directionID',0))
        categoryID = int(self.kwargs.get('categoryID',0))

        queryset = CourseModel.objects.filter(is_show=True,is_delete=False).order_by('id')

        if directionID >0:
            queryset = queryset.filter(direction=directionID)

        if categoryID >0 :
            queryset = queryset.filter(category=categoryID)

        return queryset


# 课程详情视图
class CourseDetaileView(RetrieveAPIView):
    # course = CourseModel.objects.get(id=1)
    # course.level   :  1
    # course.get_level_display()  : 中级
    queryset = CourseModel.objects.all()
    serializer_class = CourseRetrieveSerializer
    lookup_field = 'pk'
    lookup_url_kwarg = "courseID"


# 根据课程id,获取章节列表
class CourseChapterListApiView(ListAPIView):
    queryset = CourseChapterModel.objects.all()
    serializer_class = CourseChapterSerializer
    lookup_field = 'pk'
    lookup_url_kwarg = 'courseID'




